package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author xiongkunYang
 * @since 2025/05/26 18:30
 */
@RestController
@RequestMapping("/productUnit")
@RequiredArgsConstructor
public class ProductUnitController extends BaseController {

    private final IProductUnitService productUnitService;

    @Operation(summary = "获取商品单位分页列表")
    @Parameters({
            @Parameter(name = "name", description = "商品单位名称")
    })
    @GetMapping("list")
    public TableDataInfo findPage(String name) {
        super.startPage();
        List<ProductUnit> list = this.productUnitService.listByName(name);
        return super.getDataTable(list);
    }

    @Operation(summary = "获取商品单位详细信息")
    @Parameters({
            @Parameter(name = "id", description = "商品单位id")
    })
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return super.success(this.productUnitService.getById(id));
    }

    @Operation(summary = "新增商品单位")
    @Parameters({
            @Parameter(name = "productUnit", description = "商品单位信息")
    })
    @Log(title = "商品单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductUnit productUnit) {
        //设置当前用户
        productUnit.setCreateBy(SecurityUtils.getUsername());
        return super.toAjax(this.productUnitService.save(productUnit));
    }

    @Operation(summary = "修改商品单位")
    @Parameters({
            @Parameter(name = "productUnit", description = "商品单位信息")
    })
    @Log(title = "商品单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductUnit productUnit) {
        //设置当前用户
        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return super.toAjax(this.productUnitService.updateById(productUnit));
    }

    @Operation(summary = "删除商品单位")
    @Parameters({
            @Parameter(name = "ids", description = "商品单位id列表")
    })
    @Log(title = "商品单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return super.toAjax(this.productUnitService.removeByIds(ids));
    }

    @Operation(summary = "获取全部商品单位")
    @GetMapping("/getUnitAll")
    public AjaxResult selectProductUnitAll() {
        return super.success(this.productUnitService.list());
    }
}
